package com.boilermaker.lotterysystem.common.exception;

import com.boilermaker.lotterysystem.common.errorcode.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ServiceException extends RuntimeException {

    /**
     * 错误码
     *
     * @see com.boilermaker.lotterysystem.common.errorcode.ServiceErrorCodeConstants
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String msg;

    public ServiceException(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }

    /**
     * 重写getMessage方法，确保异常信息能正确打印
     */
    @Override
    public String getMessage() {
        return String.format("错误码: %d, 错误信息: %s", code, msg);
    }

    /**
     * 重写toString方法，提供更详细的异常信息
     */
    @Override
    public String toString() {
        return String.format("ServiceException{code=%d, msg='%s'}", code, msg);
    }
}
